#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
	int val;
	struct ListNode* next;
};
typedef struct ListNode ListNode;
ListNode* CreateCircleList(int n)
{
	int i = 0;
	ListNode* head = NULL, * tail = NULL;
	for (i = 1; i <= n; i++)
	{
		ListNode* ptr = (ListNode*)malloc(sizeof(ListNode));
		ptr->val = i;
		if (head == NULL)
		{
			head = tail = ptr;
			tail = ptr;
		}
		else {
			tail->next = ptr;
			tail = ptr;
		}
	}
	tail->next = head;
	return tail;
}
int ysf(int n, int m)
{
	ListNode* Pre = CreateCircleList(n);
	ListNode* Cur = Pre->next, * Next = NULL;
	int count = m-1;
	while (Pre!=Pre->next)
	{
		while (count--)
		{
			Pre = Pre->next;
			Cur = Cur->next;
			Next = Cur->next;
		}
		Pre->next = Next;
		free(Cur);
		Cur = Next;
		count = m - 1;
	}
	return Pre->val;
}
int main()
{
	int num = ysf(5, 2);
	printf("%d", num);
	return 0;
}